﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Statistic : System.Web.UI.Page
{
    List<Bill> lstBill = new List<Bill>();
    BillBUS billBUS = new BillBUS();
    DataTable tbBill = new DataTable();
    BillDetailsBUS billDetailsBUS = new BillDetailsBUS();
    DataTable tbBou = new DataTable();
    BillGiftBUS biGiftBUS = new BillGiftBUS();
    DataTable tbGift = new DataTable();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }

    protected void btnFill_Click(object sender, EventArgs e)
    {

        if (DatePicker1.SelectedDate.ToShortDateString().Equals("1/1/0001") || DatePicker2.SelectedDate.ToShortDateString().Equals("1/1/0001"))
        {
            lblError.Text = "- You must choose date";
        }
        else
        {
            DateTime fDate;
            DateTime tDate;
            string status;
            lblError.Text = "";
            fDate = DateTime.Parse(DatePicker1.SelectedDate.ToShortDateString());
            tDate = DateTime.Parse(DatePicker2.SelectedDate.ToShortDateString());
            status = drlStatus.SelectedValue.ToString();
            Session["fDate"] = fDate;
            Session["tDate"] = tDate;
            Session["status"] = status;
            LoadFill(fDate, tDate, status);
        }


    }
    public void LoadFill(DateTime fDate,DateTime tDate,string status)
    {
        lstBill = billBUS.Statistic(fDate, tDate, status);
        grvStatistic.DataSource = lstBill;
        grvStatistic.DataBind();
    }
    protected void grvStatistic_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("View"))
        {
            tbBill = billBUS.returnBillByID(Int32.Parse(e.CommandArgument.ToString()));
            lblIDMember.Text = tbBill.Rows[0]["Bi_ID"].ToString();
            lblAddress.Text = tbBill.Rows[0]["Bi_Address"].ToString();
            lblMessages.Text = tbBill.Rows[0]["Bi_Messages"].ToString();
            lblTotal.Text = tbBill.Rows[0]["Bi_TotalAmount"].ToString();
            lblStatus.Text = tbBill.Rows[0]["Bi_Status"].ToString();
            tbBou = billDetailsBUS.returnBillDetailsByID(Int32.Parse(e.CommandArgument.ToString()));
            grvBou.DataSource = tbBou;
            grvBou.DataBind();
            tbGift = biGiftBUS.returnBillGiftByBiID(Int32.Parse(e.CommandArgument.ToString()));
            if (tbGift.Rows.Count > 0)
            {
                lblGift.Visible = true;
                grvGift.DataSource = tbGift;
                grvGift.DataBind();
            }
            MultiView1.ActiveViewIndex = 1;
        }

    }

    protected void grvStatistic_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvStatistic.PageIndex = e.NewPageIndex;
        LoadFill(DateTime.Parse(Session["fDate"].ToString()), DateTime.Parse(Session["tDate"].ToString()),Session["status"].ToString());

    }
}